# -*- coding: utf-8 -*-
import pandas as pd
from PyQt6.QtWidgets import QFileDialog, QMessageBox

def download_template(parent):
    """下载Excel模板文件"""
    # 创建示例数据
    data = {
        '学院': ['示例学院1', '示例学院2'],
        '专业': ['示例专业1', '示例专业2'],
        '年级': [1, 2],
        '男生人数': [30, 25],
        '女生人数': [20, 25]
    }
    
    # 创建DataFrame
    df = pd.DataFrame(data)
    
    # 让用户选择保存位置
    file_name, _ = QFileDialog.getSaveFileName(
        parent,
        "保存Excel模板",
        "体育课程安排模板.xlsx",
        "Excel Files (*.xlsx)"
    )
    
    if file_name:
        try:
            # 保存Excel文件
            df.to_excel(file_name, index=False)
            QMessageBox.information(parent, "成功", "模板文件已保存")
        except Exception as e:
            QMessageBox.critical(parent, "错误", "保存模板文件时出错：%s" % str(e))

def import_excel(parent):
    """导入Excel文件"""
    file_name, _ = QFileDialog.getOpenFileName(
        parent,
        "选择Excel文件",
        "",
        "Excel Files (*.xlsx *.xls)"
    )
    
    if file_name:
        try:
            return pd.read_excel(file_name)
        except Exception as e:
            QMessageBox.critical(parent, "错误", "导入Excel文件时出错：%s" % str(e))
            return None
    return None 